from pprint import pprint

import pandas as pd

# 分组聚合，在pandas中抛开聚合仅分组没有意义，因此分组后必要进行聚合
col = pd.DataFrame(
    {'color': ['white', 'red', 'green', 'red', 'green'], 'object': ['pen', 'pencil', 'pencil', 'ashtray', 'pen'],
     'price1': [5.56, 4.20, 1.30, 0.56, 2.75], 'price2': [4.75, 4.12, 1.60, 0.75, 3.15]})
print(col)
"""
   color   object  price1  price2
0  white      pen    5.56    4.75
1    red   pencil    4.20    4.12
2  green   pencil    1.30    1.60
3    red  ashtray    0.56    0.75
4  green      pen    2.75    3.15
"""
data = col.groupby("color")["price1"].mean()
pprint(data)
"""
color
green    2.025
red      2.380
white    5.560
"""
data = col.groupby("color", as_index=False)["price1"].mean()
pprint(data)
"""
   color  price1
0  green   2.025
1    red   2.380
2  white   5.560
"""
